<?php
namespace App\Controller;
use Cake\Collection\Collection;

class LevelAcosController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：删除会员功能权限节点
    * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->LevelAcos->deleteAll(['id'=>$id]);
        $this->G->success("success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：修改会员功能权限节点
    * */
    public function update()
    {
        $parent_id = $this->request->getData('parent_id');
        $alias = $this->request->getData('alias');
        $memo = $this->request->getData('memo');
        $status = $this->request->getData('status');
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
//        if(empty($alias)){
//            $this->G->error("alias_error");
//            return null;
//        }
//        if(empty($acos)){
//            $this->G->error("acos_error");
//            return null;
//        }
        $levelAco = $this->LevelAcos->newEntity();
        $levelAco->id = $id;
        if(!empty($alias)){
            $levelAco->alias = $alias;
        }
        $levelAco->parent_id = empty($parent_id) ? 0:$parent_id;
        if(!empty($memo)){
            $levelAco->memo = json_encode($memo);
        }
        if(!empty($status)){
            $levelAco->status = $status;
        }
        $this->LevelAcos->save($levelAco);
        $this->G->success("Success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：新增会员功能权限节点
    * */
    public function add()
    {
        $parent_id = $this->request->getData('parent_id');
        $alias = $this->request->getData('alias');
        $memo = $this->request->getData('memo');
        $status = $this->request->getData('status');
//        if(empty($alias)){
//            $this->G->error("alias_error");
//            return null;
//        }
//        if(empty($acos)){
//            $this->G->error("acos_error");
//            return null;
//        }
        $levelAco = $this->LevelAcos->newEntity();
        if(!empty($alias)){
            $levelAco->alias = $alias;
        }
        $levelAco->parent_id = empty($parent_id) ? 0:$parent_id;
        if(!empty($memo)){
            $levelAco->memo = json_encode($memo);
        }
        if(!empty($status)){
            $levelAco->status = $status;
        }
        $this->LevelAcos->save($levelAco);
        $this->G->success("Success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：获取会员功能权限节点
    * */
    public function getList()
    {
        $levelAcos = $this->LevelAcos->find()
            ->order(['lft asc'])
            ->toArray();
        $collection = new Collection($levelAcos);
        $levelAcos = $collection->nest('id', 'parent_id')->toList();
        $this->G->success("Success", $levelAcos);
    }


}
?>